package com.szh.mysobproject.action;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.szh.mysobproject.R;
import com.szh.mysobproject.widget.MyStatusLayout;

public interface MyStatusAction {

    /**
     * 获取状态布局
     */
    MyStatusLayout getMyStatusLayout();

    /**
     * 显示加载中
     */
    default void showLoading() {
        showLoading(R.raw.loading);
    }

    default void showLoading(@RawRes int id) {
        MyStatusLayout myStatusLayout = getMyStatusLayout();
        myStatusLayout.show();
        myStatusLayout.setAnimResource(id);
        myStatusLayout.setHint("");                  //设置提示文字
        myStatusLayout.setOnRetryListener(null);
    }

    /**
     * 显示加载完成
     */
    default void showComplete() {
        MyStatusLayout myStatusLayout = getMyStatusLayout();
        if (myStatusLayout == null || !myStatusLayout.isShow()) {
            return;
        }
        myStatusLayout.hide();
    }

    /**
     * 显示空数据
     */
    default void showEmpty() {
        showLayout(R.drawable.status_empty_ic, R.string.status_layout_no_data, null);
    }

    /**
     * 显示错误事件
     *
     * @param listener 点击事件
     */
    default void showError(MyStatusLayout.OnRetryListener listener) {
        MyStatusLayout myStatusLayout = getMyStatusLayout();
        Context context = myStatusLayout.getContext();
        ConnectivityManager manager = ContextCompat.getSystemService(context, ConnectivityManager.class);
        if (manager != null) {
            NetworkInfo info = manager.getActiveNetworkInfo();
            //判断网络是否连接
            if (info == null || !info.isConnected()) {
                showLayout(R.drawable.status_nerwork_ic, R.string.status_layout_error_network, listener);
                return;
            }
        }
        showLayout(R.drawable.status_error_ic, R.string.status_layout_error_request, listener);
    }

    /**
     * 自定义提示
     *
     * @param drawableId 提示图标
     * @param stringId   提示文字
     * @param listener   点击事件
     */
    default void showLayout(@DrawableRes int drawableId, @StringRes int stringId, MyStatusLayout.OnRetryListener listener) {
        MyStatusLayout myStatusLayout = getMyStatusLayout();
        Context context = myStatusLayout.getContext();
        showLayout(ContextCompat.getDrawable(context, drawableId), context.getString(stringId), listener);
    }

    default void showLayout(Drawable drawable, CharSequence hint, MyStatusLayout.OnRetryListener listener) {
        MyStatusLayout myStatusLayout = getMyStatusLayout();
        myStatusLayout.show();
        myStatusLayout.setIcon(drawable);
        myStatusLayout.setHint(hint);
        myStatusLayout.setOnRetryListener(listener);
    }

}
